package day09;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class TestStream {

    public static void main(String[] args) {

    }

    // 题目1：文本文件/图片/视频的复制。比较不用流之间的效率
    public static void test1() {
        System.out.println("start:" + System.currentTimeMillis());
        streamCopy();
        System.out.println("end:" + System.currentTimeMillis());

        System.out.println("start:" + System.currentTimeMillis());
        bufferCopy();
        System.out.println("end:" + System.currentTimeMillis());

    }

    // 题目2：实现图片加密操作
    public static void test2() {
        File srcTextPath = new File("src/test11/EaRfXFPUYAEGtmk.jpg");
        File dirTestPath = new File("src/test11/kasimi3.jpg");
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(srcTextPath);
            os = new FileOutputStream(dirTestPath);
            int len;
            while ((len = is.read()) != -1) {
                os.write(len ^ 5);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 扩展，解密图片，在做一次异或操作就可以解密了
    public static void test21() {
        File srcTextPath = new File("src/test11/kasimi3.jpg");
        File dirTestPath = new File("src/test11/kasimi31.jpg");
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(srcTextPath);
            os = new FileOutputStream(dirTestPath);
            int len;
            while ((len = is.read()) != -1) {
                os.write(len ^ 5);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 题目3：获取文本每个字节出现的次数,存入map,并输出到文本文件当中
    public static void test3() {
        File file = new File("src/test11/hello.txt");
        File out = new File("src/test11/hello_map.txt");
        Map<Character, Integer> map = new HashMap<>();
        FileReader reader = null;
        FileWriter fw = null;
        try {
            reader = new FileReader(file);
            fw = new FileWriter(out);
            int len;
            while ((len = reader.read()) != -1) {
                char c = (char) len;
                int number;
                if (map.get(c) == null) {
                    number = 0;
                } else {
                    number = map.get(c);
                }
                map.put(c, ++number);
            }
            for (Character character : map.keySet()) {
                int number = map.get(character);
                switch (character) {
                    case ' ':
                        fw.write("空格=" +number + "\n");
                        break;
                    case '\n':
                        fw.write("换行=" +number + "\n");
                        break;
                    case '\r':
                        fw.write("回车=" +number + "\n");
                        break;
                    default:
                        fw.write(character + "=" +number + "\n");
                        break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    protected static void streamCopy() {

        File srcTextPath = new File("src/test11/EaRfXFPUYAEGtmk.jpg");
        File dirTestPath = new File("src/test11/kasimi1.jpg");
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(srcTextPath);
            os = new FileOutputStream(dirTestPath);
            int len;
            while ((len = is.read()) != -1) {
                os.write(len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    protected static void bufferCopy() {
        File srcTextPath = new File("src/test11/EaRfXFPUYAEGtmk.jpg");
        File dirTestPath = new File("src/test11/kasimi2.jpg");
        InputStream is = null;
        OutputStream os = null;
        InputStream bis = null;
        OutputStream bos = null;
        try {
            is = new FileInputStream(srcTextPath);
            os = new FileOutputStream(dirTestPath);

            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(os);

            int l;
            while ((l = bis.read()) != -1) {
                bos.write(l);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
